package com.shadow.demo.info.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.shadow.demo.common.bo.R;
import com.shadow.demo.common.info.InfoConstants;
import com.shadow.demo.common.util.ip.IPUtils;
import com.shadow.demo.data.info.entity.IpInfoEntity;
import com.shadow.demo.info.service.IpInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.SocketException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@DS("info")
@RestController
@RequestMapping("ip")
public class IpController {

    private final IpInfoService ipService;

    IpController(IpInfoService ipService) {
        this.ipService = ipService;
    }

    /**
     * ip注册
     * */
    @GetMapping("public/register/{key}/{ip}")
    public R register(
            final HttpServletRequest request,
            @PathVariable("key") String key,
            @PathVariable("ip") String ip
    ) {
        if (StringUtils.equals(ip, "0.0.0.0")) {
            String remoteAddr = request.getRemoteAddr();
            log.info("ipRegister, key: {}, remoteAddr:{}", key, remoteAddr);
            ipService.register(InfoConstants.PREFIX_REMOTE + key, key, remoteAddr);
            return R.success(remoteAddr);
        }
        ipService.register(InfoConstants.PREFIX_LOCAL + key, key, ip);
        return R.success();
    }

    /**
     * 获取调用方的公网ip
     * */
    @GetMapping("public")
    public R getPublicIp() {
        String publicIp = ipService.getPublicIp1();
        return R.success(publicIp);
    }

    @GetMapping("new")
    public R ipNew() throws SocketException {
        log.info("start get ip(new).");
        String ip = IPUtils.getAppAddressInfo().getIpAddress();
        log.info("info new result, ip : {}", ip);

        Map<String, Object> map = new HashMap<>();
        map.put( "ip", ip);
        map.put( "time", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        return R.success(map);
    }

    @GetMapping("data")
    public R ipData() {
        log.info("start get ip(data).");
        IpInfoEntity data = ipService.getLatestData(null, null);
        if (data != null) {
            log.info("info data result, ip : {}", data.getIpAddress());
            Map<String, Object> map = new HashMap<>();
            map.put( "machineName", data.getMachineName());
            map.put( "ip", data.getIpAddress());
            map.put( "time", DateFormatUtils.format(data.getAddDate(), "yyyy-MM-dd HH:mm:ss"));
            map.put("sum", data.getSum());
            return R.success(map);
        }
        return R.fail("未查询到数据");
    }
}
